package leetcode.p938;
/**
 * @author: trtan
 * @create: 2021-04-27 08:29
 * @description:
 **/
public class RangeSumOfBST {
    public int rangeSumBST(TreeNode root, int low, int high) {
        if (root == null) {
            return 0;
        }
        int result = 0;
        if (root.val >= low && root.val <= high) {
            result = root.val;
        }
        if (low <= root.val) {
            result += rangeSumBST(root.left, low, high);
        }
        if (high >= root.val) {
            result += rangeSumBST(root.right, low, high);
        }
        return result;
    }
}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
